package whu.cn.whushare.ui.main.known;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.androidplus.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

import whu.cn.whushare.R;

//弹框显示搜索历史信息
public class SearchHistoryPop {
    private PopupWindow mPop;       //一个浮动在当前activity上的容器，可以用来展示任意视图
    private Context mContext;
    private View mSearchView;
    private List<String> mAllHistories;
    private List<String> mSearchHistories = new ArrayList<>();
    private SearchHistoryAdapter mAdapter = new SearchHistoryAdapter(mContext, mSearchHistories);

    //构造函数，初始化成员变量
    public SearchHistoryPop(Context context, View searchView) {
        mContext = context;
        mSearchView = searchView;
        mAllHistories = SearchHistorySPManager.getInstance(context).getAll();
        initPop();
    }

    //初始化Pop
    private void initPop() {
        mPop = new PopupWindow(mContext);
        ListView hisView = (ListView) LayoutInflater.from(mContext).inflate(R.layout.layout_search_history, null);
        hisView.setAdapter(mAdapter);
        hisView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //todo
            }
        });
        mPop.setContentView(null);
    }

    /**
     * 方法名：show(String str)
     * 功 能：以下拉列表的形式显示包含指定字符串str的前10条搜索历史信息，若str为空，则显示前10条搜索历史信息
     * @param str - 指定的搜索字符串
     */
    public void show(String str) {
        mSearchHistories.clear();
        if (StringUtil.isNullOrEmpty(str)) {
            mSearchHistories.addAll(mAllHistories.subList(0, (mAllHistories.size() > 10) ? 10 : mAllHistories.size()));
        } else {
            for (int i = 0, n = 0; i < mAllHistories.size() && n < 10; i++) {
                if (mAllHistories.get(i).contains(str)) {
                    mSearchHistories.add(mAllHistories.get(i));
                    n++;
                }
            }
        }
        mAdapter.notifyDataSetChanged();
        if (!mPop.isShowing()) {
            mPop.showAsDropDown(mSearchView);
        }
    }

    //释放popup window
    public void dismiss() {
        mPop.dismiss();
    }

    /**
     * 将指定字符串str添加到搜索历史列表中
     * 若str为空，则直接返回；
     * 若str非空，且搜索历史列表中包含该字符串，则将该字符串添加到列表头部；
     * 若str非空，且搜索历史列表中不包含该字符串，则将该字符串添加到列表尾部；
     */
    public void addHis(String str) {
        if (StringUtil.isNullOrEmpty(str)) {
            return;
        }
        int index;
        if ((index = mAllHistories.indexOf(str)) != -1) {
            mAllHistories.remove(index);
            mAllHistories.add(0, str);
        } else {
            mAllHistories.add(str);
        }

        SearchHistorySPManager.getInstance(mContext).save(str);
        mAdapter.notifyDataSetChanged();
    }
}
